package com.feifan.monthhelper.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.feifan.monthhelper.R;

/**
 * Created on 2019/4/10.
 */
public class CommonDialog extends Dialog {

    protected TextView tvTitle;
    protected TextView tvLeft;
    protected TextView tvRight;
    protected ImageView ivDialogLogo;

    private int resId;
    private String title;
    private String leftText;
    private String rightText;

    public static final int CLICK_POS_LEFT = 0x11;
    public static final int CLICK_POS_RIGHT = 0x22;
    private OnButtonclickListener listener;

    public CommonDialog(@NonNull Context context) {
        super(context, R.style.DialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_common);
        tvTitle = findViewById(R.id.tv_title);
        tvLeft = findViewById(R.id.tv_left);
        tvRight = findViewById(R.id.tv_right);
        ivDialogLogo = findViewById(R.id.iv_dialog_logo);

        tvLeft.setOnClickListener(clickListener);
        tvRight.setOnClickListener(clickListener);
    }

    @Override
    public void show() {
        super.show();
        tvTitle.setText(title);
        tvLeft.setText(leftText);
        tvRight.setText(rightText);
        if(resId == 0) {
            ivDialogLogo.setVisibility(View.GONE);
            return;
        }
        ivDialogLogo.setVisibility(View.VISIBLE);
        ivDialogLogo.setImageResource(resId);
    }

    public static class Builder {
        private CommonDialog dialog;

        public Builder(Context context) {
            dialog = new CommonDialog(context);
        }

        public Builder setLogo(int resId) {
            dialog.resId = resId;
            return this;
        }

        public Builder setTitle(String title) {
            dialog.title = title;
            return this;
        }

        public Builder setLeftText(String leftText) {
            dialog.leftText = leftText;
            return this;
        }

        public Builder setRightText(String rightText) {
            dialog.rightText = rightText;
            return this;
        }

        public Builder setListener(OnButtonclickListener listener) {
            dialog.listener = listener;
            return this;
        }

        public void show() {
            dialog.show();
        }
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_left:
                    if (listener != null) {
                        listener.onClickButton(CLICK_POS_LEFT);
                    }
                    break;
                case R.id.tv_right:
                    if (listener != null) {
                        listener.onClickButton(CLICK_POS_RIGHT);
                    }
                    break;
            }
            dismiss();
        }
    };

    public interface OnButtonclickListener {
        void onClickButton(int buttonPos);
    }
}
